import SecureUtil from './SecureUtil'

export default {

    /**
     * 数组去重
     * @param {*} list 
     * @param {*} key 
     */
    distinct(list, key = null) {
        let newList = {}
        if (key == null) {
            for (let i = 0; i < list.length; i++) {
                const item = list[i];
                newList[SecureUtil.md5("id:"+JSON.stringify(item))] = item
            }
        } else {
            for (let i = 0; i < list.length; i++) {
                const item = list[i];
                newList[SecureUtil.md5("id:"+JSON.stringify(item[key]))] = item
            }
        }
        return Object.values(newList)
    },
    /**
     * 获取两个数组之间的差集
     * @param {*} array1 
     * @param {*} array2 
     * @param {*} key 
     */
    diff(array1, array2, key = null) {
        let newArray1 = [];
        let newArray2 = [];
        if (key == null) {
            for (let i = 0; i < array1.length; i++) {
                const item = array1[i]
                newArray1.push(SecureUtil.md5("id:"+JSON.stringify(item)))
            }
            for (let i = 0; i < array2.length; i++) {
                const item = array2[i]
                newArray2.push(SecureUtil.md5("id:"+JSON.stringify(item)))
            }
        } else {
            for (let i = 0; i < array1.length; i++) {
                const item = array1[i]
                newArray1.push(SecureUtil.md5("id:"+JSON.stringify(item[key])))
            }
            for (let i = 0; i < array2.length; i++) {
                const item = array2[i]
                newArray2.push(SecureUtil.md5("id:"+JSON.stringify(item[key])))
            }
        }
        let newArray = []

        for (let i = 0; i < newArray1.length; i++) {
            const newItem1 = newArray1[i];
            if (!newArray2.includes(newItem1)) {
                newArray.push(newItem1)
            }       
        }

        for (let i = 0; i < newArray2.length; i++) {
            const newItem2 = newArray2[i];
            if (!newArray1.includes(newItem2)) {
                newArray.push(newItem2)
            }       
        }

        if (key == null) {
            for (let i = 0; i < diffArray.length; i++) {
                const diffItem = diffArray[i]
                for (let j = 0; j < array1.length; j++) {
                    const item = array1[j];
                    if (diffItem === SecureUtil.md5("id:"+JSON.stringify(item))) {
                        newArray.push(item)
                    }
                }
                for (let j = 0; j < array2.length; j++) {
                    const item = array2[j];
                    if (diffItem === SecureUtil.md5("id:"+JSON.stringify(item))) {
                        newArray.push(item)
                    }
                }
            }
        } else {
            for (let i = 0; i < diffArray.length; i++) {
                const diffItem = diffArray[i]
                for (let j = 0; j < array1.length; j++) {
                    const item = array1[j];
                    if (diffItem === SecureUtil.md5("id:"+JSON.stringify(item[key]))) {
                        newArray.push(item)
                    }
                }
                for (let j = 0; j < array2.length; j++) {
                    const item = array2[j];
                    if (diffItem === SecureUtil.md5("id:"+JSON.stringify(item[key]))) {
                        newArray.push(item)
                    }
                }
            }
        }

        return Object.values(newArray)
    },
}